<?
function list_resellers($order,$ordertype,$limit,$skip){
	global $session_db_name, $dbh,$baseURL,$webDIR,$params;

	if($order) $order=" order by $order $ordertype";
	if($limit && $skip) $limit="limit $skip, $limit ";
	else if($limit) $limit=" limit $limit ";

	$SQL="select 
				r.id,r.reseller,r.firstname,r.lastname,r.email,	r.country,
				sum((s.type='domain')) dom,sum((s.type='domain')*(s.state='refund')) dom_refund,
				sum((s.type='plan')) plan,sum((s.type='plan')*(s.state='refund')) plan_refund,
				sum((s.price-s.real_price)*(s.state='normal')) total_profit
			from resellers r left join sales s using(id) 
			group by r.id
			$order
			$limit";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	while($row = @mysql_fetch_array($result)){
		while(list($k,$v)=each($row)) if(!$v) $row[$k]='0';	
		$params[resellername]=$row[reseller];
		$params[action]="set_reseller";
		if($row[plan_refund] || $row[dom_refund]) $st = "bgcolor='#EEEEEE'";
		else $st="";
		$list .="
		<tr $st >
			<td>$row[id]</td>
			<td><a href='$baseURL/$webDIR/?".encode_params($params)."'>$row[reseller]</a></td>
			<td>$row[firstname] $row[lastname]</td>
			<td><a href='#'>$row[email]</a></td>
			<td>$row[country]</td>
			<td>$row[plan]</td>
			<td>$row[plan_refund]</td>
			<td>$row[dom]</td>
			<td>$row[dom_refund]</td>
			<td>$row[total_profit]</td>
		 </tr>";
  }
  return $list;
}
function list_resellers_domains($order,$ordertype,$limit,$skip,$domain=''){
	global $session_db_name, $dbh,$baseURL,$webDIR,$params;

	if($order) $order=" order by $order $ordertype";
	if($limit && $skip) $limit="limit $skip, $limit ";
	else if($limit) $limit=" limit $limit ";
	if($domain) $where=" and domain='$domain'";
	$SQL="select	r.id,r.reseller,r.email,d.domain
			 from resellers r left join domains d using(id) where d.domain!='' $where 
			$order
			$limit";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	while($row = @mysql_fetch_array($result)){
		foreach($row as $v) if(!$v) $v="l"; 
		$params[resellername]=$row[reseller];
		$params[action]="set_reseller";
		$list .="
		<tr>
			<td>$row[id]</td>
			<td><a href='$baseURL/$webDIR/?".encode_params($params)."'>$row[reseller]</a></td>
			<td><a href='#'>$row[email]</a></td>
			<td><a href='http://$row[domain]' target='_blank'>$row[domain]</a></td>
		 </tr>";
  }
  return $list;
}

function get_hosting_packets($type="array"){
        include("../hosting_connect/call.php");
        $func[function_name]="get_packet_names";
        $hosting_packets=call_remote_method($func);

	if($type=="select"){
		foreach($hosting_packets as $p){
			$select.="<option value='$p'>$p</option>\n";
		}
		return $select;
	}
	return $hosting_packets;

}
function get_packets(){
	global $session_db_name, $dbh,$baseURL,$webDIR,$sid;
	
	$select =get_hosting_packets("select");
	$SQL="select plan, description, alias, setup_fee_max, plan_min_price, plan_real_price, plan_max_price, domain_min_price, domain_real_price, domain_max_price from plans;";
	
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$params[sid]=$sid;
	$params[action]="edit_packet";
	while($row = @mysql_fetch_array($result)){
		$params[plan]=$row[plan];
		$res.="<form action='$baseURL/$webDIR/?".encode_params($params)."' method='POST'><tr>
			<td>$row[plan]</td>
			<td><textarea name='description' rows='4'>$row[description]</textarea></td>
			<td><select name=alias><option value='$row[alias]'>$row[alias]</option>$select</select></td>
			<td><input type='text' size=6 name='setup_fee_max' value='$row[setup_fee_max]'></td>
			<td><input type='text' size=6 name='plan_min_price' value='$row[plan_min_price]'></td>
			<td><input type='text' size=6 name='plan_real_price' value='$row[plan_real_price]'></td>
			<td><input type='text' size=6 name='plan_max_price' value='$row[plan_max_price]'></td>
			<td><input type='text' size=6 name='domain_min_price' value='$row[domain_min_price]'></td>
			<td><input type='text' size=6 name='domain_real_price' value='$row[domain_real_price]'></td>
			<td><input type='text' size=6 name='domain_max_price' value='$row[domain_max_price]'></td>
			<td><input type='submit' value='Change'></td>
			</tr>
			</form>
			";
	}
	return $res;
}


function change_packet($plan, $description, $alias, $setup_fee_max, $plan_min_price, $plan_real_price, $plan_max_price, $domain_min_price, $domain_real_price, $domain_max_price){
	global $session_db_name, $dbh;
	$SQL="update plans set 
		description='$description',
		alias='$alias',
		setup_fee_max='$setup_fee_max',
		plan_min_price='$plan_min_price',
		plan_real_price='$plan_real_price',
		plan_max_price='$plan_max_price',
		domain_min_price='$domain_min_price',
		domain_real_price='$domain_real_price',
		domain_max_price='$domain_max_price'
		
		where plan='$plan'
		";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	return 1;
}

function add_packet($plan, $description, $alias, $setup_fee_max, $plan_min_price, $plan_real_price, $plan_max_price, $domain_min_price, $domain_real_price, $domain_max_price){
	global $session_db_name, $dbh;
	$SQL="insert into plans(plan,description,alias,setup_fee_max,plan_min_price,plan_real_price,plan_max_price,domain_min_price,domain_real_price,domain_max_price)values('$plan','$description','$alias','$setup_fee_max','$plan_min_price','$plan_real_price','$plan_max_price','$domain_min_price','$domain_real_price','$domain_max_price')";

	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	return 1;

}

function search_resellers($username,$id,$mail,$firstname,$lastname,$country,$state,$ip,$client,$domain){
	global $session_db_name, $dbh,$baseURL,$webDIR,$sid;
	$where = " where ";
	if($username)   {$data.= " $where r.reseller like '%$username%'"; $where = " and ";}
	if($id)               {$data.= " $where r.id = '$id' "; $where = " and ";}
	if($mail)               {$data.= " $where r.email like '%$mail%'"; $where = " and ";}
	if($country)    {$data.= " $where r.country like '%$country%'"; $where = " and ";}
        if($state)    {$data.= " $where r.state like '%$state%'"; $where = " and ";}
	if($firstname)   {$data.= " $where r.firstname like '%$firstname%'"; $where = " and ";}
	if($lastname)   {$data.= " $where r.lastname like '%$lastname%'"; $where = " and ";}
	if($client)   {$data.= " $where s.client like '%$client%'"; $where = " and ";}
	if($ip)                 {$data.=" $where ip = '$ip'"; $where = " and ";}
	if($domain) {$data.=" $where d.domain = '$domain'"; $where = " and ";}
	
	
	if(!$data) return 0;

	$SQL = "select distinct r.id,r.blocked,r.w9_form,r.reseller,r.firstname,r.lastname,r.email,r.country,r.state,r.ip,r.date from resellers r left join sales s using(id) left join domains d on(r.id=d.id) $data";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$list = '';
	while($row = @mysql_fetch_array($result)){
		$params[sid]=$sid;
		$params[action]="set_reseller";
		$params[resellername] = $row[reseller];
		$params[res_id] = $row[id];
		$url="$baseURL/$webDIR/?".encode_params($params);
		$params[action]="view_groups";
		$url_affiliates="$baseURL/$webDIR/payments/?".encode_params($params);

                $params[action]="repair";
                $url_repair="$baseURL/$webDIR/?".encode_params($params);

		$params[action]='block_reseller';
		$params[id] = $row[id];
		if($row[blocked]=='active'){
			$button_value="block";
			$params[blocked]='blocked';			
		}else{
			$button_value="activate";
			$params[blocked]='active';
		}
		$url_block="$baseURL/$webDIR/?".encode_params($params);
		$params[action]='w9_submit';
		$w9_value="W9";
		$url_w9="$baseURL/$webDIR/?".encode_params($params);
		
		$list .="
		<tr>
			<td>$row[id]</td>
			<td><b><a href='$url'>$row[reseller]</a></b></td>
			<td>$row[firstname]</td>
			<td>$row[lastname]</td>
			<td>$row[email]</td>
			<td>$row[country]</td>
			<td>$row[state]</td>
			<td>$row[ip]</td>
			<td>$row[date]</td>
			<td><input type='button' value='$button_value' onCLick=\"window.open('$url_block',null,'width=320,height=160,status=no,resizable=0,scrollbars=1')\"></td>
			<td><input type='button' value='$w9_value' onCLick=\"window.open('$url_w9',null,'width=320,height=160,status=no,resizable=0,scrollbars=1')\"></td>
			<td><input type='button' value='affiliates' onCLick=\"window.open('$url_affiliates')\"></td>
			<td><input type='button' value='Repair' onCLick=\"window.open('$url_repair')\"></td>
		</tr>";
	}
	return $list;
}

function set_mail($file,$subject,$body){
	global $baseDIR, $rpType;
	if(!$file) return 0;
	if(!$subject) return 0;
	if(!$body) return 0;
	if ($rpType == 'ninja') {
	        if (get_magic_quotes_gpc()) {
	                $subject = stripslashes($subject);
	                $body = stripslashes($body);
        	}

	        $subject = trim($subject);


	        $fp = fopen("$baseDIR/admin/mails/$file", "w");
	        if (!$fp) return 0;
        	fputs($fp, "Subject: $subject\n");
	        fputs($fp, "$body");
	        fclose($fp);

        	return 1;
	} else {
		$body = str_replace("\\","",$body);
		system("/usr/local/admin/admin/setmail.pl  \"$file\" \"$subject\" \"$body\"",$err);

		if($err <> 0){
			echo "ERROR : $err";
			exit();
			return 0;
		}
		return 1;
	}
}
//----------------------------------------------------------------------------------
function get_mail($filename){
	global $baseDIR;
	if(!$filename) return 0;
	$filename = "$baseDIR/admin/mails/".$filename;
	if(file_exists($filename)){
		$arr = file($filename);
		$buff = explode(":",$arr[0]);
		$mail[subject] = $buff[1];
		$mail[subject] = trim($mail[subject]);
		$arr[0] = "";
		foreach($arr as $a){
			if(substr($a,0,1) != ".") {
//                              $a = str_replace("\\","",$a);
				$mail[body] .= $a;
			}
		}
	}
	else {
		$mail[subject] = "ERROR";
		$mail[body] = "ERROR";
	}
	return $mail;
}

function search_client($client,$reseller){
	global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
	$SQL="select distinct s.client from sales s left join resellers r using( id) where s.client like '%$client%' and r.reseller like '%$reseller%';";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$params[sid]=$sid;
	$params[action]="client_refund";
        while($row = @mysql_fetch_array($result)){
		$params[client]=$row[client];
		$url="$baseURL/$webDIR/?".encode_params($params);
		$res.="<tr><td><a href='$url'>$row[client]</a></td><tr>";
	}
	return $res;
}
function get_client_refund_details($client){
	global $session_db_name, $dbh,$sid,$baseURL,$webDIR;

	$SQL="select s.order_number,s.sale_id,s.client,r.reseller,s.type,s.state,s.refund_type,s.payed,s.refund_comment from sales s left join resellers r using (id) where s.client='$client';";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	while($row = @mysql_fetch_array($result)) {
		$res[]=$row;
	}
	return $res;
}


function make_refund($client,$comment,$refund_type,$sale_ids){
	global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
	foreach($sale_ids as $k=>$v){
		$where[] = "sale_id = '$v'";
	}
	$where = implode(" or ",$where);
	if($refund_type=='normal'){
		 $SQL="update sales set state='normal', refund_type='$refund_type', refund_comment='$comment' where client='$client' AND (".$where.")";
	}else $SQL="update sales set state='refund', refund_type='$refund_type', refund_comment='$comment' where client='$client' AND (".$where.")";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;

	return 1;
}
function get_last_sales($s=''){
	global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
	if(is_array($s)){
		if($s[reseller]) $ggg[]="r.reseller like '$s[reseller]%'";
		if($s[client]) $ggg[]="s.client like '$s[client]%'";
		if($s[date]) $ggg[]="s.date like '$s[date]%'";
		if($s[order_number]) $ggg[]="s.order_number like '$s[order_number]%'";
		if(is_array($ggg))$where.="where " . implode(" and ",$ggg);
	}
       if(is_array($s)) $limit = "";
       else  $limit = "limit 50";

	$SQL="select r.reseller,s.sale_id, s.client,s.type,s.price,s.real_price,s.payed,s.state,s.refund_type,s.payment_date,s.date,s.order_number from sales s left join resellers r using(id) $where order by s.date desc $limit";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$params[sid]=$sid;
	while($row = @mysql_fetch_array($result)) {
		if(!$row[payment_date] )$row[payment_date]  ="~";
		if(!$row[order_number] )$row[order_number]  ="~";
		if(!$row[type] )$row[type]  ="~";
		$params[resellername]=$row[reseller];
		$params[action]="set_reseller";
		$bgcolor='';
		$refund_type = "";
		if($row[state]=='refund'){ 
			if($row[refund_type] == 'charge_back') $bgcolor="bgcolor=#E89898";
			else $bgcolor="bgcolor=#EFEFEF";
			if($row[refund_type])$refund_type = "<br><font size='-2'>$row[refund_type]</font>";
		}
		$res.="<tr $bgcolor>
			<td><a href='$baseURL/$webDIR/?".encode_params($params)."'>$row[reseller]</a></td>
			";
                $params[action]='get_cclog_info';
                $params[order_number] = "$row[order_number]";
		$more_link = "$baseURL/$webDIR/?".encode_params($params);
		$params[action]='client_refund';
		$params[client]=$row[client];
		$res.="	
			<td><a href='$baseURL/$webDIR/?".encode_params($params)."'>$row[client]</a></td>
			<td>$row[type]</td>
			<td>$row[price]</td>
			<td>$row[real_price]</td>
			<td>$row[state] $refund_type </td>
			<td>$row[date]</td>
			<td><a href='#' onClick=\"open_page('$more_link');\">more</a></td>
			<td>$row[order_number]</td>";

        	$params[action]="delete_sale";
		$params[sale_id]=$row[sale_id];
		$url="$baseURL/$webDIR/?".encode_params($params);
		
		$res.="<form action='$url' method=POST><td><input type=submit value=Del></td></form>";
		$res.="</tr>";	
	}
	return $res;
}
function delete_sale($sale_id){
	global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
	$SQL="delete from sales where sale_id='$sale_id'";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	else return 1;
}
function get_last_reseller_sales($s=''){
	global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
	if(is_array($s)){
		if($s[reseller]) $ggg[]="r.reseller like '$s[reseller]%'";
		if($s[date]) $ggg[]="s.date like '$s[date]%'";
		if($s[order_number]) $ggg[]="s.order_number like '$s[order_number]%'";
		if($s[data]) $ggg[]="s.data like '$s[data]%'";
		
		if(is_array($ggg))$where.="where " . implode(" and ",$ggg);
	}
	$SQL="select r.reseller, s.type,s.price,s.date,s.data,s.order_number from sales_resellers s left join resellers r on(s.reseller_id=r.id) $where order by s.date desc limit 50";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$params[sid]=$sid;
	while($row = @mysql_fetch_array($result)) {
		if(!$row[order_number] )$row[order_number]  ="~";
		$params[resellername]=$row[reseller];
		$params[action]="set_reseller";
		$bgcolor='';
		$res.="<tr $bgcolor>
			<td><a href='$baseURL/$webDIR/?".encode_params($params)."'>$row[reseller]</a></td>
			";
                $params[action]='get_cclog_info';
                $params[order_number] = "$row[order_number]";
		$more_link = "$baseURL/$webDIR/?".encode_params($params);
		$res.="	
			<td>$row[type]</td>
			<td>$row[data]</td>
			<td>$row[price]</td>
			<td>$row[date]</td>
			<td><a href='#' onClick=\"open_page('$more_link');\">more</a></td>
			<td>$row[order_number]</td>
		</tr>";	
	}
	return $res;
}
function get_last_payments(){
        global $session_db_name, $dbh;
        $SQL="	select	r.id,r.reseller,r.firstname,r.lastname,r.email, r.country,s.payed,s.payment_date,sum((s.price-s.real_price)*(s.state='normal')) cheque	from resellers r left join sales s using(id) where s.payed='true' group by r.id,s.payment_date order by payment_date desc;";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        while($row = @mysql_fetch_array($result)) {
		$res.="<tr>
			<td>$row[reseller]</td>
			<td>$row[firstname] $row[lastname]</td>
			<td><a href='mailto:$row[email]'>$row[email]</a></td>
			<td>$row[country]</td>
			<td>$row[payment_date]</td>
			<td>$row[cheque]</td>
		</td>";
		
        }
        return $res;
}

function block_reseller($resellername,$blocked){
	global $session_db_name, $dbh;
	$SQL="update resellers set blocked='$blocked' where reseller='$resellername'";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	return 1;
}
function w9_form_submit($resellername,$w9){
	global $session_db_name, $dbh;
	$SQL="update resellers set w9_form='$w9' where reseller='$resellername'";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	return 1;
}
function w9_get_count($id){
        global $session_db_name, $dbh;
        $SQL="select count(*) as count from w9 where id='$id';";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $row = @mysql_fetch_array($result);
        return $row[count];
}

function get_affiliates_data($reseller){
	global $session_db_name, $dbh;
	
	$SQL="select r.id,rr.id as aff_id,rr.reseller,rr.firstname,rr.lastname,rr.email,rr.country,rr.state,rr.address1,rr.address2 from resellers r left join referers rf on(r.reseller=rf.referer and rf.referer_type=1 and rf.type='signup') left join resellers rr on(rf.id=rr.id) where r.reseller='$reseller';";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	while($row=@mysql_fetch_assoc($result)){
		$tables[table_1].="<tr>
			<td><input type='checkbox' name='affiliates[]' value='$row[aff_id]'></td>
			<td>$row[aff_id]</td>
			<td>$row[reseller]</td>
			<td>$row[firstname]</td>
			<td>$row[lastname]</td>
			<td>$row[email]</td>
			<td>$row[country]</td>
			<td>$row[state]</td>
			<td>$row[address1], $row[address1]</td>
		</tr>";
		$id=$row[id];
	}

	$SQL="select rf.referer,r.id,r.reseller,r.firstname,r.lastname,r.email,r.country,r.state,r.address1,r.address2 from referers rf left join resellers r on(rf.referer=r.reseller and rf.type='signup' and rf.referer_type=1) where rf.id='$id';";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	while($row=@mysql_fetch_assoc($result)){
		$tables[table_2].="<tr>
                        <input type='hidden' name='resellerid' value='$id'>
			<td>$row[reseller]</td>
                        <td>$row[firstname]</td>
                        <td>$row[lastname]</td>
                        <td>$row[email]</td>
                        <td>$row[country]</td>
			<td>$row[state]</td>
                        <td>$row[address1], $row[address2]</td>
		
		</tr>";
	}
	if(!$tables[table_1]) $tables[table_1]='<tr><td>this reseller has not refered anyone</td></tr>';
	if(!$tables[table_2]) $tables[table_2]='<tr><td colspan=10>no referer</td></tr>';
	$tables[resellerid]=$id;
	return $tables;

}

function remove_affiliates($affiliates){
	global $session_db_name, $dbh;
	
	if(!is_array($affiliates)) return 0;
	$where="where id='".  implode("' or id='",$affiliates) ."' and type='signup'";
	if(!$where) return 0;
	$SQL="delete from  referers $where";
	return  mysql_db_query($session_db_name,$SQL, $dbh);
}
function remove_referer($id){
	global $session_db_name, $dbh;
	if(!$id) return 0;
	$SQL="delete from referers where id='$id' and type='signup'";
	return  mysql_db_query($session_db_name,$SQL, $dbh);
}
function add_affiliate($resellername,$affiliate_id){
	global $session_db_name, $dbh;
	$SQL="select ip,date from resellers where id='$affiliate_id'";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$row=@mysql_fetch_assoc($result);
	if(!$row[ip]) return 0;
	$SQL="insert into referers (referer,type,date,id,ip,referer_type) values('$resellername','signup','$row[date]','$affiliate_id','$row[ip]',1)";
	return mysql_db_query($session_db_name,$SQL, $dbh);
}
function add_referer($resellername,$referer){
	global $session_db_name, $dbh;
	$SQL="select ip,id,date from resellers where reseller='$resellername'";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$row=@mysql_fetch_assoc($result);
	
	if(!$row[ip]) return 0;
	if(!$row[id]) return 0;
	$SQL="insert into referers (referer,type,date,id,ip,referer_type) values ('$referer','signup','$row[date]','$row[id]','$row[ip]',1)";
	return mysql_db_query($session_db_name,$SQL, $dbh);
}
function search_temporary_signup($arr){
	global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
	if(!is_array($arr)) return 0;
	while(list($k,$v)=each($arr)) if($v) $arr1[$k]="$k like '%$v%'";
	$arr1[]="signup_state='signup_begin'";
	$where="where ". implode(" and ",$arr1);
	$SQL=	"select 
			t.reseller_id,
			t.sid,
			t.firstname,
			t.lastname,
			t.email,
			t.username,
			t.address,
			t.domain,
			t.plan,
			t.ip,
			c.billingAction
		from temporary_signup t left join creditlog c on(t.sid=c.sid)$where limit 50";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
	$params[sid]=$sid;
	$params[action]="broken_billing";
	$url="$baseURL/$webDIR/?".encode_params($params);

	while($row=mysql_fetch_assoc($result)){
		$res.="<form action='$url' method=post><tr>";
//		foreach($row as $r) $res.="<td>$r</td>";
		$res.="
			<td>$row[reseller_id]</td>
			<td>$row[firstname]</td>
			<td>$row[lastname]</td>
			<td>$row[email]</td>
			<td>$row[username]</td>
			<td>$row[domain]</td>
			<td>$row[plan]</td>
			<td>$row[ip]</td>
		";
		$res.="<input type=hidden name='sida' value='$row[sid]'>";
		$res.="<td>B:<input size=8 type=text name='b_act' value='$row[billingAction]'></td>";
		$res.="<td><input size=10  type=text name='order_number'></td>";
		$res.="<td><input type='submit' value='Signup'></td>";
		$res.="	</tr></form>";

	}
	return $res;
}

function search_temporary_register_domain($arr){
        global $session_db_name, $dbh,$sid,$baseURL,$webDIR;
        if(!is_array($arr)) return 0;
        while(list($k,$v)=each($arr)) if($v) $arr1[$k]="$k like '%$v%'";
        $arr1[]="register_domain_state='register_domain_begin'";

        $where="where ". implode(" and ",$arr1);
        $SQL=   "select
                        t.reseller_id,
                        t.sid,
                        t.firstname,
                        t.lastname,
                        t.email,
                        t.address1,
                        t.domain,
                        c.billingAction
                from temporary_register_domain t left join creditlog c on(t.sid=c.sid) $where limit 50";
	if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        $params[sid]=$sid;
        $params[action]="broken_billing";
        $url="$baseURL/$webDIR/?".encode_params($params);

        while($row=mysql_fetch_assoc($result)){
                $res.="<form action='$url' method=post><tr>";
//              foreach($row as $r) $res.="<td>$r</td>";
                $res.="
                        <td>$row[reseller_id]</td>
                        <td>$row[firstname]</td>
                        <td>$row[lastname]</td>
                        <td>$row[email]</td>
                        <td>$row[domain]</td>
                        
                ";
                $res.="<input type=hidden name='sida' value='$row[sid]'>";
                $res.="<input type=hidden name='b_act' value='register_domain'>";
                $res.="<td><input size=10  type=text name='order_number'></td>";
                $res.="<td><input type='submit' value='Signup'></td>";
                $res.=" </tr></form>";

        }
        return $res;
}
//----------------------------------------
function add_comment($id,$comment){
        global $session_db_name, $dbh;
        $SQL="insert into comments values('$id',now(),'$comment')";
        return  mysql_db_query($session_db_name,$SQL, $dbh);
}
//----------------------------------------
function get_comment($id){
        global $session_db_name, $dbh;
        $SQL="select date,comment from comments where id='$id'";
        if (!($result = mysql_db_query($session_db_name,$SQL, $dbh))) return 0;
        while($row=@mysql_fetch_assoc($result)){
        	$list .= "<tr><th>$row[date]</th><th>$row[comment]</th></tr>";
        }
        return $list;
}
//----------------------------------------
//----------------------------------------
function get_cclog_info($order_number){
        global $session_db_name, $dbh;
        $SQL="SELECT * from cclog where order_number='$order_number' limit 1;";
	if (($result = mysql_db_query($session_db_name,$SQL, $dbh))){
		$row=@mysql_fetch_assoc($result);
		$list .= "<tr><td colspan='2'>CC LOG</tr>";
		if (is_array($row)) while(list($k,$v) = each($row)){
			 if($k=='username'){
				 $username=$v;
				 $list .= "<tr><th>$k : </th><td> <a target='_blank' href='http://www.lonex.com/admin/memberlogin/index.php?pich=cbfl&username=$username'>$v</a></td></tr>";
			 }
			 else $list .= "<tr><th>$k : </th><td>$v</td></tr>";		
		}
	}
        $SQL="SELECT * from temporary_signup WHERE username='$username' and signup_state='signup_ok'";
        if (($result = mysql_db_query($session_db_name,$SQL, $dbh))){
        	$row=@mysql_fetch_assoc($result);
		 $list .= "<tr><td colspan='2'>SIGNUP</tr>";
	       if(is_array($row))
			 while(list($k,$v) = each($row)){
        	         $list .= "<tr><th>$k : </th><td>$v</td></tr>";
	        }
	}
        return $list;

}
//----------------------------------------


?>
